package edu.stanford.prpl.impl;

import java.io.InputStream;
import java.util.Calendar;

import edu.stanford.prpl.api.BlobResource;
import edu.stanford.prpl.api.Device;
import edu.stanford.prpl.api.PRPLObjectManager;
import edu.stanford.prpl.api.PRPLObjectManager.BlobManager;
import edu.stanford.prpl.common.PRPLConstants;

public class BlobResourceImpl extends ResourceImpl implements BlobResource {

	public static final String BLOB_MIMETYPE_URI = PRPLConstants.PRPL_PREFIX + "mimeType";
	public static final String BLOB_CONTENT_HASH_URI = PRPLConstants.PRPL_PREFIX + "contentHash";
	public static final String BLOB_SOURCE_URI = PRPLConstants.PRPL_PREFIX + "sourceURI";
	public static final String BLOB_SIZE_URI = PRPLConstants.PRPL_PREFIX + "size";
	public static final String BLOB_MODIFIED_DATE_URI = PRPLConstants.PRPL_PREFIX + "blobModifiedDate";

	BlobManager blobManager;

	public BlobResourceImpl(String Uri, PRPLObjectManager objManager) {
		super(Uri, objManager);
		this.blobManager = objManager.getBlobManager();
	}

	public InputStream getBlob() {
		// String blobServerURL = this.blobManager.getResourceBlobLocation(resourceURI, BlobManager.BLOB_OP_READ);
		Device device = this.blobManager.getResourceBlobServerDevice(objectURI);
		return this.blobManager.getResourceBlob(device.getServerURL(), objectURI);
	}

	
	public Calendar getBlobLastModifiedDate() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(this.blobManager.getResourceBlobLastModifiedDate(objectURI));
		return cal;
	}

	
	public String getContentHash() {
		return this.blobManager.getResourceBlobContentHash(objectURI);
	}

	
	public String getMimeType() {
		return this.blobManager.getResourceBlobMimeType(objectURI);
	}

	
	public long getSize() {
		return this.blobManager.getResourceBlobSize(objectURI);
	}

	
	public String getSourceURI() {
		return this.blobManager.getResourceBlobSourceURI(objectURI);
	}

	
	public void setBlob(InputStream inputstream) {
		this.blobManager.setResourceBlob(objectURI, inputstream);
	}

	
	public void setBlobLastModifiedDate(Calendar date) {
		this.blobManager.setResourceBlobLastModifiedDate(objectURI, date.getTimeInMillis());

	}

	
	public void setContentHash(String contentHash) {
		this.blobManager.setResourceBlobContentHash(objectURI, contentHash);
	}

	
	public void setMimeType(String mimeType) {
		this.blobManager.setResourceBlobMimeType(objectURI, mimeType);
	}

	
	public void setSize(long size) {
		this.blobManager.setResourceBlobSize(objectURI, size);
	}

	
	public void setSourceURI(String sourceURI) {
		this.blobManager.setResourceBlobSourceURI(objectURI, sourceURI);
	}

	
	public byte[] getBlob(int startPosition, int len) {
		return this.blobManager.getResourceBlob(objectURI, startPosition, len);
	}

	
	public void setBlob(int startPosition, byte[] data) {
		this.blobManager.setResourceBlob(objectURI, startPosition, data);
	}

}
